var utils = require('./exports/utils')
var constant = require('./exports/constant')
var point_map = require('./exports/point_map')
var ad_util = require('./exports/ad_util')
var ocrUtils = require('./exports/ocrUtils')
//任务 看广告赚钱
var DELTA_X = 598 - 127;
var DELTA_Y = 948 - 926;
function ad_click_ad() {
  console.log("ad_click_ad");
  var ui_item = utils.exists_text("看广告赚钱");
  var ui_item_bounds = ui_item.bounds();
  var rate_x = device.width / 720;
  var rate_y = device.height / 1650;
  var x = ui_item_bounds.centerX() + DELTA_X * rate_x;
  var y = ui_item_bounds.centerY() + DELTA_Y * rate_y;
  utils.click_x_y(parseInt(x), parseInt(y));
}

var ad = {
  go_page_from_home() {
    if (
      !!utils.exists_text("百度") &&
      !!utils.exists_text("视频") &&
      !!utils.exists_text("小说")
    ) {
      console.log("ad:go_page_from_home");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页任务"])
      );
      sleep(constant.min_click_after_time * 1000);
      this.start();
    } else {
      console.log("ad:go_page_from_home: NOT FOUND");
    }
  },
  start() {
    console.log("ad start");
    this.init();
  },
  init() {
    var ui_text = utils.exists_text_starts_with("看广告赚钱");
    if (ui_text) {
      var ui_text_bounds = ui_text.bounds();
      if (
        device.height - ui_text_bounds.centerY() >
        parseInt((device.height / 1650) * 116)
      ) {
        console.log("ad init: 找到【看广告赚钱】");
        ad_click_ad();
        sleep(constant.min_click_after_time * 1000);
        ad_util.execute();
        this.next();
      } else {
        console.log("ad init: 今天已经执行完【看广告赚钱】");
        this.next();
      }
    } else {
      console.log("ad init: 没找到【看广告赚钱】");
      this.next();
    }
  },
  next() {
    console.log("ad next");
    if (utils.click_text("立即收下")) {
      console.log("ad next: 找到【立即收下】");
      sleep(constant.min_click_after_time * 1000);
    }
    if (utils.click_text("开心收下")) {
      console.log("ad next: 找到【开心收下】");
      sleep(constant.min_click_after_time * 1000);
    }
  },
};

ad.start()